<template>
    <!-- 卡片 -->
    <el-card>
        <!---具名 slot-->
        <template #header>
            <div>
                <span>基本资料</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form :model="userInfo" :rules="rules" label-width="100px" size="large">
                    <el-form-item label="用户账号">
                        <el-input v-model="userInfo.account" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="用户名称" prop="name">
                        <el-input v-model="userInfo.name"></el-input>
                    </el-form-item>
                    <el-form-item label="用户密码" prop="password">
                        <el-input v-model="userInfo.password"></el-input>
                    </el-form-item>

                    <el-form-item label="性别">
                        <el-radio-group v-model="userInfo.sex">
                            <el-radio-button label="男" value="男" />
                            <el-radio-button label="女" value="女" />
                        </el-radio-group>
                    </el-form-item>

                    <el-form-item label="联系方式" prop="phone">
                        <el-input v-model="userInfo.phone"></el-input>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="updateStaff">提交修改</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>

<script setup>
import { ref } from 'vue'
import { managerPiniaInfo } from "@/stores/managerPinia";
const managerInfo = managerPiniaInfo();
import managerApi from '@/api/manager';
import { ElNotification } from 'element-plus';

//定义数据模型
const userInfo = ref({ ...managerInfo.managerInfo })



//参数校验
const rules = {
    name: [
        { required: true, message: '请输入用户名称', trigger: 'blur' },
        { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入用户密码', trigger: 'blur' },
        { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
    ],
    phone: [
        { required: true, message: '请输入用户联系方式', trigger: 'blur' },
        { min: 11, max: 11, message: '请输入11位联系方式', trigger: 'blur' }
    ]

}

//获取用户信息
const getStaffInfo = () => {
    managerApi.getStaffById(userInfo.value.id).then(res => {
        if (res.data.code == 0) {
            managerInfo.setManagerInfo(res.data.data)
            ElNotification({
            title: 'Success',
            message: '修改成功',
            type: 'success',
        })
        }
    })
   
}

//修改用户信息
const updateStaff = () => {
    if (userInfo.value.name != '' && userInfo.value.password != '' && userInfo.value.phone.length == 11) {
        managerApi.managerUpdateStaff(userInfo.value).then(res => {
            if (res.data.code == 0) {
                console.log("修改成功")
                //修改成功
                //重新获取用户信息
                getStaffInfo()
            }
        })
    } else {
        ElNotification({
            title: 'Error',
            message: '请输入完整信息',
            type: 'error',
        })
    }
}
</script>